﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.AspNet.Membership.OpenAuth;
using System.Data.SqlClient;
using System.Data;

public partial class Account_Register : Page
{
    public int CurrentOrderId;
    protected void Page_Load(object sender, EventArgs e)
    {
        //RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];
    }

    protected void RegisterUser_CreatedUser(object sender, EventArgs e)
    {
        //FormsAuthentication.SetAuthCookie(RegisterUser.UserName, createPersistentCookie: false);

        //string continueUrl = RegisterUser.ContinueDestinationPageUrl;
        //if (!OpenAuth.IsLocalUrl(continueUrl))
        //{
           // continueUrl = "~/";
        //}
        //Response.Redirect(continueUrl);
    }

    protected void RegisterUser_CreatingUser(object sender, LoginCancelEventArgs e)
    {
        //RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];
        //var idx = 1;
    }

    private bool IsExistsUser() {
        var isExists = true;
        string name = UserName.Text;

        using (SqlConnection sqlcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ShopConnectionString"].ConnectionString))
        {
            SqlCommand com = new SqlCommand("select * from Users where UserName = '" + name + "'" , sqlcon);
            DataSet dataSet = new DataSet();
            DataTable dataTable = new DataTable();
            SqlDataAdapter dataAdapter = new SqlDataAdapter();
            dataAdapter.SelectCommand = com;
            dataAdapter.Fill(dataTable);
            dataSet.Tables.Add(dataTable);
            if (dataTable.Rows.Count == 0) isExists = false; 
        }
       
        return isExists;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if(this.IsValid)
        {

            if (IsExistsUser())
            {
                //    //this.IsValid = false;
            }
            else
            {
                var CurrentUserId = 0;
                using (SqlConnection sqlcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ShopConnectionString"].ConnectionString))
                {
                    SqlCommand cmd = new SqlCommand("InsertUser", sqlcon);
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add(new SqlParameter("@Status", SqlDbType.Int));
                    cmd.Parameters["@Status"].Value = 1;
                    cmd.Parameters.Add(new SqlParameter("@Password", SqlDbType.NVarChar));
                    cmd.Parameters["@Password"].Value = Password.Text;
                    cmd.Parameters.Add(new SqlParameter("@Email", SqlDbType.NVarChar));
                    cmd.Parameters["@Email"].Value = Email.Text;
                    cmd.Parameters.Add(new SqlParameter("@UserName", SqlDbType.NVarChar));
                    cmd.Parameters["@UserName"].Value = UserName.Text;

                    cmd.Parameters.Add(new SqlParameter("@FirstName", SqlDbType.NVarChar, 50));
                    cmd.Parameters["@FirstName"].Value = FirstName.Text;
                    cmd.Parameters.Add(new SqlParameter("@LastName", SqlDbType.NVarChar, 50));
                    cmd.Parameters["@LastName"].Value = LastName.Text;
                    cmd.Parameters.Add(new SqlParameter("@Id", SqlDbType.Int, 4));
                    cmd.Parameters["@Id"].Direction = ParameterDirection.Output;
                    sqlcon.Open();
                    try
                    {
                        cmd.ExecuteNonQuery();
                        CurrentUserId = Convert.ToInt32(cmd.Parameters["@Id"].Value);
                    }
                    finally
                    {
                        sqlcon.Close();
                    }
                    //AddImages(CurrentOrderId);
                }
                //}
                Session["User"] = UserName.Text;
                Session["UserId"] = CurrentUserId;
                Response.Redirect("~/Default.aspx");

            }
        }
    }
}